<template>
    <div>
        <h-section title="JIRA需求" collapse :defaultOpen="!!list.length || !disabled" style="padding-top: 0" ref="relateProduct">
            <custom-table ref="ruleTableRef" class="margin-t-16" size="small" rowKey="id" :pagination="false" :columns="columns" :data-source="list">
                <template #summary="{ text }">
                    <span>{{ text }}</span>
                </template>
                <template #sprint="{ text }">
                    <span>{{ text }}</span>
                </template>
                <template #aggregateTimeOriginalEstimate="{ text }">
                    {{ $filters.secondToDay(text) }}
                </template>
                <template #aggregateTimeSpent="{ text }">
                    {{ $filters.secondToDay(text) }}
                </template>
            </custom-table>
        </h-section>
    </div>
</template>

<script lang="ts">
import api from '@/api';
import store from '@/store';
import { message } from 'ant-design-vue';
import { CustomTable } from '@/components';
import { defineComponent, ref, unref, reactive, toRefs, watch, computed } from 'vue';
export default defineComponent({
    components: {
        CustomTable
    },
    props: {
        detail: { type: Object, default: () => ({}) },
        disabled: { type: Boolean, default: false },
        stepStatus: { type: String, default: '' }
    },
    emits: ['success'],
    setup(props, { emit }) {
        const state = reactive<any>({
            list: [],
            selectedRow: {}, //选择行
            visible: false,
            changeOpinion: ''
        });
        const columns = [
            {
                title: '需求名称',
                dataIndex: 'summary',
                width: 300,
                ellipsis: true,
                slots: { customRender: 'summary' }
            },
            {
                title: '关键字',
                dataIndex: 'issueKey',
                width: 100
            },
            {
                title: 'issue 状态',
                dataIndex: 'status',
                width: 100
            },
            {
                title: '需求类型',
                dataIndex: 'issueType',
                width: 80
            },
            {
                title: 'sprint',
                align: 'center',
                dataIndex: 'sprint',
                ellipsis: true,
                width: 150,
                slots: { customRender: 'sprint' }
            },
            {
                title: 'SP',
                width: 100,
                align: 'center',
                dataIndex: 'storyPoints',
                slots: { customRender: 'storyPoints' }
            },
            {
                title: '预估工时',
                width: 100,
                align: 'center',
                dataIndex: 'aggregateTimeOriginalEstimate',
                slots: { customRender: 'aggregateTimeOriginalEstimate' }
            },
            {
                title: '实际耗时',
                width: 100,
                align: 'center',
                dataIndex: 'aggregateTimeSpent',
                slots: { customRender: 'aggregateTimeSpent' }
            }
        ];
        const handleOk = async () => {
            if (state.visible === true && !state.changeOpinion) {
                message.error('调整原因不能为空');
                return;
            }
            state.visible = false;
            console.log('selectedRow', state.selectedRow);
            const { processStatus } = props.detail;
            const { empId, name } = store.getters.userInfo.employeeDTO;
            const query = [
                {
                    issueId: state.selectedRow.id,
                    component: state.selectedRow.component,
                    projectKey: state.selectedRow.projectKey,
                    operation: 'delete',
                    versionId: props.detail.id,
                    catalogCode: props.detail.pmpProductDetailDTO.catalogCode,
                    record: ['3', '4', '5', '6', '7', '8', '9'].includes(processStatus), // 789(验收，上线审核和上线流程)需要记录需求列表操作
                    empId,
                    empName: name,
                    processStatus,
                    taskNode: props.stepStatus,
                    changeOpinion: state.changeOpinion
                }
            ];
            const { success, errorMsg } = await api.jiraIssueModel.batchDeal(query);
            if (success) {
                message.success('移除成功');
                emit('success');
            } else {
                message.error(errorMsg);
            }
        };
        const handleDelete = (row) => {
            state.selectedRow = row;
            if (props.detail.pmpJiraIssueDTOs.length === 1 && props.stepStatus === 'requireOnline') {
                message.warn('这是该版本唯一的需求，不可移除');
                return;
            }
            if (+props.detail.processStatus >= 3) {
                state.visible = true;
                state.changeOpinion = '';
            } else {
                handleOk();
            }
        };
        const init = () => {
            state.list = props.detail.pmpJiraIssueDTOs || [];
        };
        init();
        watch(() => props.detail, init, { deep: true });
        return {
            columns,
            ...toRefs(state),
            handleDelete
        };
    }
});
</script>

<style scoped>
</style>